package com.ziyougou.coupon.api;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ziyougou.common.to.BoundsTo;
import com.ziyougou.common.to.MemberPrice;
import com.ziyougou.common.to.SkusTo;
import com.ziyougou.coupon.entity.MemberPriceEntity;
import com.ziyougou.coupon.entity.SkuFullReductionEntity;
import com.ziyougou.coupon.entity.SkuLadderEntity;
import com.ziyougou.coupon.entity.SpuBoundsEntity;
import com.ziyougou.coupon.service.MemberPriceService;
import com.ziyougou.coupon.service.SkuFullReductionService;
import com.ziyougou.coupon.service.SkuLadderService;
import com.ziyougou.coupon.service.SpuBoundsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/coupon")
public class CouponApi {
    @Autowired
    private MemberPriceService memberPriceService;
    @Autowired
    private SkuFullReductionService skuFullReductionService;
    @Autowired
    private SkuLadderService skuLadderService;


    //保存优惠价格相关信息
    @PostMapping("/save/skureduction")
    public void saveReduction(@RequestBody SkusTo skusTo){
            //1.保存会员相关信息 member_price表
        List<MemberPrice> memberPriceList = skusTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntitiesList=
                memberPriceList.stream().map(memberPrice -> {
                    MemberPriceEntity priceEntity = new MemberPriceEntity();
                    priceEntity.setSkuId(skusTo.getSkuId());
                    priceEntity.setMemberLevelId(memberPrice.getId().longValue());
                    priceEntity.setMemberLevelName(memberPrice.getName());
                    priceEntity.setMemberPrice(new BigDecimal(memberPrice.getPrice()+""));
                    return priceEntity;}).collect(Collectors.toList());

        memberPriceService.saveBatch(memberPriceEntitiesList);
        //2.保存满减数据: sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skusTo.getSkuId());
        skuFullReductionEntity.setFullPrice(new BigDecimal(skusTo.getFullPrice()+""));
        skuFullReductionEntity.setReducePrice(new BigDecimal(skusTo.getReducePrice()+""));
        skuFullReductionEntity.setAddOther(skusTo.getPriceStatus());
        skuFullReductionService.save(skuFullReductionEntity);
        //3.保存打折数据: sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skusTo.getSkuId());
        skuLadderEntity.setFullCount(skusTo.getFullCount());
        skuLadderEntity.setDiscount(new BigDecimal(skusTo.getDiscount()+""));
        skuLadderEntity.setPrice(new BigDecimal(skusTo.getPrice()+""));
        skuLadderEntity.setAddOther(skusTo.getCountStatus());
        skuLadderService.save(skuLadderEntity);
    }

    @Autowired
    private SpuBoundsService boundsService;
    //保存积分信息
    @PostMapping("/save/bounds")
    void saveBounds(@RequestBody BoundsTo boundsTo){
        System.out.println(boundsTo.getBuyBounds());
        SpuBoundsEntity spuBoundsEntity = new SpuBoundsEntity();
        BeanUtils.copyProperties(boundsTo,spuBoundsEntity);
        boundsService.save(spuBoundsEntity);
    }
}
